<?php
/** 
 * Member_model
 * This class handles model work necessary for logging in user
 * 
 * @author Ray Nowell
 *	
 */ 
class Member_model extends CI_Model {

	function Member_model()
	{
		parent::__construct();
                $this->load->library('encrypt');
	}
	
	/*
	 * Returns a member record based on the currently logged in member id 
	 */
	function Load_member_info($member_id)
	{	
		$query = $this->db->limit(1)->get_where('member', array('member_id' => $member_id));
		return $query->row();
	}

	function Update_member($data)
	{
		$this->db->where('member_id', $data['member_id']);
		return $this->db->update('member',$data);
	}
    
    function Delete_member_car($car_id)
	{
		$this->db->delete('member_car', array('car_id' => $car_id)); 
	}
    
	function Get_member_car_history($member_id) 
	{
		$select_fields =	"	 member_car.car_id
                                ,CONCAT('<img src=\"./member_car_gallery/thumbnail/',pic_name,'_thumb.jpg\"/>') as pic_name 
								,nickname
								,year
								,model.short_name as model
								,engine.short_name as engine
								,DATE_FORMAT(owned_from,'%c/%e/%Y') AS owned_from
								,DATE_FORMAT(owned_to,'%c/%e/%Y') AS owned_to
								,CASE for_sale WHEN 1 THEN 'Yes' Else 'No' END as for_sale";
		$this->db->select($select_fields, false);
		$this->db->from('member_car');
		$this->db->join('member_car_gallery', 'member_car.car_id = member_car_gallery.car_id and member_car_gallery.is_display_photo = 1','left');
		$this->db->join('lookup AS model', 'member_car.model_id = model.lookup_id', 'left'); 
		$this->db->join('lookup AS engine', 'member_car.engine_id = engine.lookup_id', 'left'); 
		$this->db->where('member_id',$member_id);
		$query = $this->db->get();
		 
		return $query->result_array();
	}
    
    function Get_member_car_photos($car_id)
	{
		$select_fields =	"	 mcg_id
								,car_id
								,pic_name
								,is_display_photo
								";
		$this->db->select($select_fields, false);
		$this->db->from('member_car_gallery');
		$this->db->where('car_id', $car_id);
        $query = $this->db->get();
		 
		return $query->result_array();
	}
	
    function Get_member_list()
	{
		$select_fields =	"	 member_id
								,CONCAT_WS(' ',first_name,last_name) AS display_name
								,IF (profile_pic = '', 'no_prof_thumb.jpg', REPLACE(profile_pic, '.jpg', '_thumb.jpg')) AS profile_pic 
								";
		$this->db->select($select_fields, false);
		$this->db->from('member');
		$this->db->where('active', true);
		$this->db->order_by('display_name','asc');
        $query = $this->db->get();
		 
		return $query->result_array();
	}
}

/* End of file member_model.php */
/* Location: ./system/application/models/member_model.php */